class JQRecordListService extends global.JQService {
	
    async do_action(){
		
		if(!this.conn)console.log('do_action::conn error');
		let data_obj = this.req_obj.data_obj;
		let inst = global.JQTableIF.instance('record_table',this.conn);
		
	  
	   
		let wrap = {};
		console.log('do_action 11111');

		// let menu_id = data_obj?data_obj.menu_id:null;
		let params=[];
		let type = data_obj.type;
		this.req_obj.visitor_id;
		let keys =[];
		let group_by=null;
		let order_by=null;
		params.push(['visitor_id=',this.req_obj.visitor_id])
		console.log('do_action 22222');
		let list =[];
		if(!type)type=0;
		let batis = global.JQSqlBaseIF.instance('record_batis/daily',this.conn);
		if(type==0){
			
			let days = await batis.days();
			keys =['ifnull(sum(amount),0) amount'];
			console.log(JSON.stringify(days))
			for(let i =0;i<days.length;i++){
				let day = days[i];
				
				let item = await inst.queryOne(keys,
				[params,['to_days(time)=',day.days]],null,null);
				if(item){
					item.quantum=day.quantum;
					item.visitor_id=this.req_obj.visitor_id;
				}
				list.push(item);
				
			}
			console.log(JSON.stringify(list))
			 
		}else if(type==1){
			let weeks = await batis.weeks();
			keys =['ifnull(sum(amount),0) amount'];
			console.log(JSON.stringify(weeks))
			for(let i =0;i<weeks.length;i++){
				let week = weeks[i];
				
				let item = await inst.queryOne(keys,
				[params,["DATE_FORMAT(time,'%y-%V')=",week.weeks]],null,null);
				if(item){
					item.quantum=week.quantum;
					item.visitor_id=this.req_obj.visitor_id;
				}
				list.push(item);
				
			}
			console.log(JSON.stringify(list))
		}else if(type==2){
			let months = await batis.months();
			keys =['ifnull(sum(amount),0) amount'];
			console.log(JSON.stringify(months))
			for(let i =0;i<months.length;i++){
				let month = months[i];
				
				let item = await inst.queryOne(keys,
				[params,["DATE_FORMAT(time,'%y-%m')=",month.months]],null,null);
				if(item){
					item.quantum=month.quantum;
					item.visitor_id=this.req_obj.visitor_id;
				}
				list.push(item);
				
			}
			console.log(JSON.stringify(list))
		}else if(type==3){
			let years = await batis.years();
			keys =['ifnull(sum(amount),0) amount'];
			console.log(JSON.stringify(years))
			for(let i =0;i<years.length;i++){
				let year = years[i];
				
				let item = await inst.queryOne(keys,
				[params,["DATE_FORMAT(time,'%Y')=",year.years]],null,null);
				if(item){
					item.quantum=year.quantum;
					item.visitor_id=this.req_obj.visitor_id;
				}
				list.push(item);
				
			}
			console.log(JSON.stringify(list))
		}
		
		
		let first= data_obj.first;
		let size = data_obj.size;	
	
	   console.log('record list ' + JSON.stringify(list));
		   
		wrap.list=list;
		keys =['sum(amount) total','visitor_id','year(time) quantum'];
		let total = await inst.queryOne(keys,params,null,null);	
		 
		// let group_by=['year(time)'];
		// let order_by='year(time) desc';
		if(total && total.total)
			wrap.total=total.total;
		else wrap.total=0;
		wrap.trees = wrap.total/100.0;
			
		return wrap;			
	}

}
module.exports = JQRecordListService;


